package com.hkts.naturalgas.base.service.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hkts.naturalgas.base.entity.CalcinationDesulfurizationDenitration;
import com.hkts.naturalgas.base.entity.CalcinationPurificationBleaching;
import com.hkts.naturalgas.base.entity.CalcinationPurifyWetElectricity;
import com.hkts.naturalgas.base.entity.HktsTailGas;
import com.hkts.naturalgas.base.mapper.CalcinationDesulfurizationDenitrationMapper;
import com.hkts.naturalgas.base.mapper.CalcinationPurifyWetElectricityMapper;
import com.hkts.naturalgas.base.service.ICalcinationPurifyWetElectricityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hkts.naturalgas.model.ResultData;
import com.hkts.naturalgas.utils.resultMapUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.apache.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yck
 * @since 2021-05-31
 */
@Service
@Transactional
public class CalcinationPurifyWetElectricityServiceImpl extends ServiceImpl<CalcinationPurifyWetElectricityMapper, CalcinationPurifyWetElectricity> implements ICalcinationPurifyWetElectricityService {


    @Autowired
    private CalcinationPurifyWetElectricityMapper calcinationPurifyWetElectricityMapper;
    @Autowired
    private CalcinationDesulfurizationDenitrationMapper cddMapper;

    @Override
    public IPage<CalcinationPurifyWetElectricity> selectPage(Page<CalcinationPurifyWetElectricity> page, CalcinationPurifyWetElectricity calcinationPurifyWetElectricity){
            QueryWrapper<CalcinationPurifyWetElectricity> query=Wrappers.query();
            QueryWrapper<CalcinationPurifyWetElectricity> entityWrapper=getEntityWrapper(query, calcinationPurifyWetElectricity);
            return calcinationPurifyWetElectricityMapper.selectPage(page,entityWrapper);
    }


    @Override
    public List<CalcinationPurifyWetElectricity> selectList(CalcinationPurifyWetElectricity calcinationPurifyWetElectricity){
            QueryWrapper<CalcinationPurifyWetElectricity> query=Wrappers.query();
            QueryWrapper<CalcinationPurifyWetElectricity> entityWrapper=getEntityWrapper(query, calcinationPurifyWetElectricity);
            return calcinationPurifyWetElectricityMapper.selectList(entityWrapper);
    }

    /**
     *  公共查询条件
     * @param entityWrapper
     * @return
     */
    public QueryWrapper<CalcinationPurifyWetElectricity> getEntityWrapper(QueryWrapper<CalcinationPurifyWetElectricity> entityWrapper,CalcinationPurifyWetElectricity calcinationPurifyWetElectricity){
            //条件拼接
            return entityWrapper;
    }
    @Override
    public IPage<CalcinationPurifyWetElectricity> selectPageByTime(Page<CalcinationPurifyWetElectricity> page, String startDate, String endDate) {
        QueryWrapper<CalcinationPurifyWetElectricity> query = Wrappers.query();
        query.lambda().apply(!"".equals(startDate),
                "time >= '" + startDate + "'")
                .apply(!"".equals(endDate),
                        "time <= '" + endDate + "'")
                .orderByDesc(CalcinationPurifyWetElectricity::getTime);
        IPage cpwePage = calcinationPurifyWetElectricityMapper.selectPage(page, query);
        List<CalcinationPurifyWetElectricity> records = cpwePage.getRecords();
        List<Map<String, Object>> collect = records.stream().map(t -> assemblyCpweData(t)).collect(Collectors.toList());
        cpwePage.setRecords(collect);
        return cpwePage;
    }
    public Map<String,Object> assemblyCpweData(CalcinationPurifyWetElectricity vo){
        Map<String,Object> data = new HashMap<>();
        /*QueryWrapper<CalcinationDesulfurizationDenitration> query = Wrappers.query();
        query.lambda().apply(!"".equals(vo.getTime()),
                "time = '" + vo.getTime() + "'");
        CalcinationDesulfurizationDenitration calcinationDesulfurizationDenitration = cddMapper.selectOne(query);
        if (calcinationDesulfurizationDenitration != null){
            data.put("dataSo2",calcinationDesulfurizationDenitration.getDataSo2());
            data.put("dataNox",calcinationDesulfurizationDenitration.getDataNox());
            data.put("dataO2",calcinationDesulfurizationDenitration.getDataO2());
            data.put("dataFc",calcinationDesulfurizationDenitration.getDataFc());
        }else {
            data.put("dataSo2",null);
            data.put("dataNox",null);
            data.put("dataO2",null);
            data.put("dataFc",null);
        }*/

        data.put("dsjhSda1",vo.getDsjhSda1());
        data.put("dsjhSda2",vo.getDsjhSda2());
        data.put("dsjhSdb1",vo.getDsjhSdb1());
        data.put("dsjhSdb2",vo.getDsjhSdb2());
        data.put("time",vo.getTime());

        return data;
    }


    @Override
    public IPage<CalcinationPurifyWetElectricity> getBrokenLineGraph(Page<CalcinationPurifyWetElectricity> page, String startDate, String endDate){
        QueryWrapper<CalcinationPurifyWetElectricity> query = Wrappers.query();
        query.lambda().apply(!"".equals(startDate),
                "time >= ' " + startDate + " ' " )
                .apply(!"".equals(endDate),
                        "time <= ' " + endDate + " ' ")
                .orderByAsc(CalcinationPurifyWetElectricity::getTime);
        IPage calcinationDesulfurizationDenitrations = calcinationPurifyWetElectricityMapper.selectPage(page,query);
        List<CalcinationPurifyWetElectricity> records = calcinationDesulfurizationDenitrations.getRecords();
        List<Map<String,Object>> resultDate = records.stream().map(t -> assemblyCargoData(t)).collect(Collectors.toList());
        Map<String, List<Object>> stringListMap = resultMapUtils.resultData(resultDate);
        List<Object> list1 = resultMapUtils.getList(resultDate);
        List<Object> listName = resultMapUtils.getListName(resultDate);
        List<Object> swasp = resultMapUtils.swasp(listName, 1, 3);
        Map<String,Object> data = new HashMap<>();
        data.put("list",swasp);
        data.put("records",stringListMap);
        data.put("times",list1);
        List<Object> resultList = new ArrayList<>();
        resultList.add(data);
        IPage iPage = calcinationDesulfurizationDenitrations.setRecords(resultList);
        return iPage;
    }

    public Map<String,Object> assemblyCargoData(CalcinationPurifyWetElectricity vo){
        Map<String,Object> data = new LinkedHashMap<>();
        /*QueryWrapper<CalcinationDesulfurizationDenitration> query = Wrappers.query();
        query.lambda().apply(!"".equals(vo.getTime()),
                "time = '" + vo.getTime() + "'");
        CalcinationDesulfurizationDenitration calcinationDesulfurizationDenitration = cddMapper.selectOne(query);
        if (calcinationDesulfurizationDenitration != null){
            data.put("二氧化硫实测浓度(mg/m³)",calcinationDesulfurizationDenitration.getDataSo2());
            data.put("氮氧化物实测浓度(mg/m³)",calcinationDesulfurizationDenitration.getDataNox());
            data.put("氧含量平均值(%)",calcinationDesulfurizationDenitration.getDataO2());
            data.put("烟尘实测浓度(mg/m³)",calcinationDesulfurizationDenitration.getDataFc());
        }else {
            data.put("二氧化硫实测浓度(mg/m³)",0.0);
            data.put("氮氧化物实测浓度(mg/m³)",0.0);
            data.put("氧含量平均值(%)",0.0);
            data.put("烟尘实测浓度(mg/m³)",0.0);
        }*/
        data.put("煅烧一电场二次电流(A)",vo.getDsjhSda1());
        data.put("煅烧一电场二次电压(V)",vo.getDsjhSda2());
        data.put("煅烧二电场二次电流(A)",vo.getDsjhSdb1());
        data.put("煅烧二电场二次电压(V)",vo.getDsjhSdb2());
        data.put("time",vo.getTime());
        return data;
    }

    @Override
    public void deleteAll() {
        calcinationPurifyWetElectricityMapper.deleteAll();
    }

    @Override
    public CalcinationPurifyWetElectricity selectByDateAndTime(String s, String stringCellValue) {
        String substring = stringCellValue.substring(0, stringCellValue.length() - 3);
        QueryWrapper<CalcinationPurifyWetElectricity> query = Wrappers.query();
        query.like(CalcinationPurifyWetElectricity.TIME,s+" "+substring);
        List<CalcinationPurifyWetElectricity> calcinationPurifyWetElectricities = calcinationPurifyWetElectricityMapper.selectList(query);
        if(calcinationPurifyWetElectricities.size()==0){
            return  null;
        }else if(calcinationPurifyWetElectricities.size()>1){
            for(int i=1;i<calcinationPurifyWetElectricities.size();i++){
                calcinationPurifyWetElectricityMapper.deleteById(calcinationPurifyWetElectricities.get(i).getId());
            }
        }
       return calcinationPurifyWetElectricities.get(0);
    }

    @Override
    public List<CalcinationPurifyWetElectricity> selectListByTime(LocalDateTime startTime, LocalDateTime endTime,String bs) {
        QueryWrapper<CalcinationPurifyWetElectricity> query = Wrappers.query();
        query.ge(CalcinationPurifyWetElectricity.TIME, startTime);
        query.le(CalcinationPurifyWetElectricity.TIME, endTime);
        if(bs.equals("1")){
            query.like(CalcinationPurifyWetElectricity.TIME, "59:59");
        }

        List<CalcinationPurifyWetElectricity> calcinationPurifyWetElectricities = calcinationPurifyWetElectricityMapper.selectList(query);
        return calcinationPurifyWetElectricities;
    }
}
